package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.Apply;
import com.lczc.soa.domain.SearchUserResponse;
import com.lczc.soa.domain.User;
import com.lczc.soa.domain.UserExample;
import com.lczc.soa.mapper.ApplyMapper;
import com.lczc.soa.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ApplyMapper applyMapper;

    @Override
    public int saveUser(User user) {
//        System.out.println("Service:"+user.getUserName());
        if(StringUtils.isNotBlank(user.getUserId())){
            return userMapper.updateByPrimaryKeySelective(user);

        }else {
            return userMapper.insert(user);
        }
    }

    @Override
    public SearchUserResponse findAllUser(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectByExample(new UserExample());
        return new SearchUserResponse(((Page)users).getTotal(),users);
    }

    @Override
    public User findUserByPrimaryKey(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteUser(String id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SearchUserResponse findUserLikeName(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectByLikeName(keywords);
        return new SearchUserResponse(((Page)users).getTotal(),users);
    }

    @Override
    public List<User> listUser() {
        return userMapper.selectAll();
    }

    @Override
    public User findUserByName(String userName) {
        return userMapper.selectByName(userName);
    }

    @Override
    public int saveApply(Apply apply) {
        return applyMapper.insert(apply);
    }

    @Override
    public int updatePassword(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

}
